﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace LCrs.Web.news
{
    /// <summary>
    /// GetNews 的摘要说明
    /// </summary>
    public class GetNews : IHttpHandler, IRequiresSessionState
    {
        Maticsoft.BLL.news nm = new Maticsoft.BLL.news();

        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            string newsId = "";
            if (null != context.Request.QueryString["newsId"])
            {
                newsId = context.Request.QueryString["newsId"].ToString().Trim();
            }
           
            if (ac == "query")
            {
                Query(context,"通知公告");
            }              
            if (ac == "add")
            {
                add(context, "通知公告");
            }
            if (ac== "modify")
            {
                modify(context);
            }
            if (ac== "delete")
            {
                delete(context);
            }
            if (ac == "querymodel")
            {
                querymodel(newsId,context);
            }
            if (ac == "import")
            {
                importFiles(context);
            }
            if (ac == "queryfilesearch")
            {
                Query(context,"文件检索");
            }
            if (ac == "addfilesearch")
            {
                add(context, "文件检索");
            }
        }

        private void querymodel(string newsId,HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                //string uname, utruename, st;
                //uname = utruename = st = "";
                //if (null != context.Request.Form["search_type"])
                //{//获取前台传来的值  
                //    st = context.Request.Form["search_type"].ToString().Trim();
                //}
                //if (null != context.Request.Form["search_value"])
                //{//获取前台传来的值  
                //    uname = context.Request.Form["search_value"].ToString().Trim();
                //}
                //if (st == "username")
                //{
                //    utruename = uname;
                //    uname = "";
                //}
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                if (newsId != "")
                {
                    strWhere.AppendFormat(" newsId = '{0}' and ", newsId);
                }
                //if (utruename != "")
                //{
                //    strWhere.AppendFormat(" pname like '%{0}%' and ", utruename);
                //}
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = nm.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = nm.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
        }

        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["newsId"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);

                if (nm.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                int newsId = 0;
                string newsIdstr = context.Request.Form["newsId"];
                if (newsIdstr != "")
                    newsId = int.Parse(newsIdstr);
                string newsUrl = context.Request.Form["newsUrl"];
                string newsTime = context.Request.Form["newsTime"];
                string newsTitle = context.Request.Form["newsTitle"];
                string newsContent = context.Request.Form["newsContent"];
                string newsAuthor = context.Request.Form["newsAuthor"];

                Maticsoft.Model.news model = new Maticsoft.Model.news();
                model.newsId = newsId;

                if (!string.IsNullOrEmpty(newsTime))
                {
                    model.newsTime = DateTime.Parse(newsTime);
                }
                else
                {
                    model.newsTime = DateTime.Now;
                }
                model.newsTitle = newsTitle;
                model.newsUrl = newsUrl;
                model.newsContent = newsContent;
                model.newsAuthor = newsAuthor;

                if (nm.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }

        private void Query(HttpContext context,string newsType)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                //string uname, utruename, st;
                //uname = utruename = st = "";
                //if (null != context.Request.Form["search_type"])
                //{//获取前台传来的值  
                //    st = context.Request.Form["search_type"].ToString().Trim();
                //}
                //if (null != context.Request.Form["search_value"])
                //{//获取前台传来的值  
                //    uname = context.Request.Form["search_value"].ToString().Trim();
                //}
                //if (st == "username")
                //{
                //    utruename = uname;
                //    uname = "";
                //}

                string newsTitle, newsContent,type, value;
                type = value = newsTitle = newsContent = "";
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    type = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    value = context.Request.Form["search_value"].ToString().Trim();
                }

                if (type == "newsTitle")
                {
                    newsTitle = value;
                }
                if (type == "newsContent")
                {
                    newsContent = value;
                }

                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
              
                if (newsTitle !="")
                {
                    strWhere.AppendFormat(" newsTitle like '%{0}%' and ", newsTitle);
                }
                if (newsContent != "")
                {
                    strWhere.AppendFormat(" newsContent like '%{0}%' and ", newsContent);
                }
                if (newsType != "")
                {
                    strWhere.Append(" newsType='" + newsType + "' and ");
                }
               
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = nm.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = nm.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
        }

        public void add(HttpContext context,string newsTypes)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string newsContent = context.Request.Form["newsContent"];
                string newsTitle = context.Request.Form["newsTitle"];
                string newsAuthor = context.Request.Form["newsAuthor"];
                string newsUrl = importFiles(context);
                string newsType = newsTypes;
              
                DateTime? newsTime = null;
                if (!string.IsNullOrEmpty(context.Request.Form["newsTime"]))
                {
                    newsTime = DateTime.Parse(context.Request.Form["newsTime"]);
                }
                else
                {
                    newsTime = DateTime.Now;
                }

                Maticsoft.Model.news model = new Maticsoft.Model.news();
                model.newsAuthor = newsAuthor;
                model.newsContent = newsContent;
                model.newsTitle = newsTitle;
                model.newsUrl = newsUrl;
                model.newsType = newsType;
                if (newsTime != null)
                {
                    model.newsTime = newsTime;
                }
               
                int rid = nm.Add(model);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public string importFiles(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string result = "";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string savepath = "";
                HttpFileCollection files = context.Request.Files;
                if (files.Count == 1)
                {
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)
                    {
                        //全路径  
                        string FullFullName = file.FileName;
                        //获取图片的名称  
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                        string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        string path = "~/UploadFiles";
                        savepath = "/UploadFiles";

                        file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                        savepath += "/" + filenames + exp;

                        result = savepath;

                        //result = "上传附件成功！";

                        //if (exp == ".xls")
                        //{

                        //}
                        //else
                        //{
                        //    result = "模版文件类型不对！";

                        //}
                        //result = ImportXlsToData(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                        //MyTools.WriteLog("导出人员档案", Web.Admin.myinfo.getID(context), context);

                    }
                }

            }
            else
            {
                result = "";
            }
            //context.Response.Write(result);//返回给前台页面  
            //context.Response.End();

            return result;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}